using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace Programonica.Igramonica.LandscapeClasses
{
    public class LandscapePrimitiveNode : LandscapeNode
    {
        private LOD m_Lod = LOD.None;

        private Dictionary<LOD, int> m_StartIndexes = new Dictionary<LOD, int>();
        private Dictionary<LOD, int> m_PrimitiveCount = new Dictionary<LOD, int>();

        protected PrimitiveList m_Primitives = new PrimitiveList();

        public LandscapePrimitiveNode()
            : base()
        {

        }

        public virtual void Build(PrimitiveList primitives)
        {
            m_Primitives = primitives;

            if ((m_Primitives != null) && (m_Primitives.Count > 0))
            {
                m_BoundingBox = m_Primitives.BoundingBox;

                m_NodeCenter = Vector3.Divide(m_BoundingBox.Max + m_BoundingBox.Min, 2.0f);
            }
        }

        public override void PrepareForDrawing()
        {
            m_Lod = LOD.None;

            m_Drawn = false;
        }

        public override LandscapeNode[] GetNodesToDraw(LOD lod)
        {
            if (!m_Drawn)
            {
                if (NodeIsInLODFrustum(lod))
                {
                    m_Lod = lod;

                    m_Drawn = true;

                    return new LandscapeNode[] { this };
                }
            }

            return new LandscapeNode[] { };
        }

        public int GetStartIndex(LOD lod)
        {
            return m_StartIndexes[lod];
        }

        public int GetPrimitiveCount(LOD lod)
        {
            return m_PrimitiveCount[lod];
        }

        internal void SetRenderParams(LOD lod, int startIndex, int primitiveCount)
        {
            if (m_StartIndexes.ContainsKey(lod))
            {
                m_StartIndexes[lod] = startIndex;
            }
            else
            {
                m_StartIndexes.Add(lod, startIndex);
            }

            if (m_PrimitiveCount.ContainsKey(lod))
            {
                m_PrimitiveCount[lod] = primitiveCount;
            }
            else
            {
                m_PrimitiveCount.Add(lod, primitiveCount);
            }
        }
    }
}
